<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\helper\Time;

class Outsideschool extends Backend
{
    protected $model = null;


    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\admin\model\Outsideschool;
        if($this->auth->id==1877){
            $this -> whereExtend['gate_id'] = 11;
        }
        // 校友
        if($this->auth->id==1889){
            $this -> whereExtend['is_schoolfriend'] = 1;
        }

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where($this -> whereExtend)
                ->order($sort, $order)
                ->count();

            $list = $this->model ->with($this->relationtTable)
                ->where($where)
                ->where($this -> whereExtend)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->fetch();
    }

    public function export()
    {
        ini_set('memory_limit', '999M');
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $rows = $this->model
            ->where($this->whereExtend)
            ->select();
        $fp = fopen('php://output', 'a+');
        header("Content-Type:application/vnd.ms-excel;charset=utf-8");
        header("Content-Disposition:filename=校外人员数据导出.csv");

        $title = array('序号','申请人名称','校外人员姓名','专业','班级','身份证','手机号','工作单位','入校事由','入校开始时间','入校截止时间','接待单位','审批时间','审批人名称','使用时间','进入的校门','车牌号');
        foreach($title as $k => $v) {
            $title[$k] = iconv('UTF-8', 'GBK', $v);
        }
        fputcsv($fp, $title);
        $i = 1;
        foreach($rows as $val){
            $row[0] = iconv('UTF-8', 'GBK', $i++);
            $row[1] = mb_convert_encoding(trim($val['applier_name']), 'GBK', 'UTF-8');
            $row[2] = mb_convert_encoding(trim($val['nickname']), 'GBK', 'UTF-8');
            
            $row[3] = mb_convert_encoding(trim($val['zy']), 'GBK', 'UTF-8');
            $row[4] = mb_convert_encoding(trim($val['bj']), 'GBK', 'UTF-8');

            $row[5] = mb_convert_encoding(trim('`'.$val['card_id']), 'GBK', 'UTF-8');
            $row[6] = mb_convert_encoding(trim('`'.$val['tel']), 'GBK', 'UTF-8');

            $row[7] = mb_convert_encoding(trim($val['work_unit']), 'GBK', 'UTF-8');
            $row[8] = mb_convert_encoding(trim($val['memo']), 'GBK', 'UTF-8');
            $row[9] = mb_convert_encoding(trim($val['admission_starttime']), 'GBK', 'UTF-8');

            $row[10] = mb_convert_encoding(trim($val['admission_endtime']), 'GBK', 'UTF-8');
            $row[11] = mb_convert_encoding(trim($val['reception_name']), 'GBK', 'UTF-8');

            $row[12] = mb_convert_encoding(trim($val['check_time_text']), 'GBK', 'UTF-8');
            $row[13] = mb_convert_encoding(trim($val['check_cn']), 'GBK', 'UTF-8');

            $row[14] = mb_convert_encoding(trim($val['use_time_text']), 'GBK', 'UTF-8');
            $row[15] = mb_convert_encoding(trim($val['gate_name']), 'GBK', 'UTF-8');
            $row[16] = mb_convert_encoding(trim($val['car_number']), 'GBK', 'UTF-8');

            fputcsv($fp, $row);
        }
        ob_flush();
        flush();
    }




}